package com.example.qxfw.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GeoJSONMinMaxUtil {
    public static void main(String[] args) {
        try {
            //修改这里更换文件路径运行即可
            File file = new File("src/main/resources/file/玉林市.json");
            String jsonString = readJSONFile(file);

            JSONObject jsonObject = JSON.parseObject(jsonString);
            JSONArray features = jsonObject.getJSONArray("features");

            List<List<List<double[]>>> allCoordinates = new ArrayList<>();
            for (int i = 0; i < features.size(); i++) {
                JSONObject feature = features.getJSONObject(i);
                JSONObject geometry = feature.getJSONObject("geometry");
                String type = geometry.getString("type");
                JSONArray coordinates = geometry.getJSONArray("coordinates");

                // 根据不同类型提取坐标信息
                if ("Polygon".equals(type)) {
                    allCoordinates.addAll(parsePolygonCoordinates(coordinates));
                } else if ("MultiPolygon".equals(type)) {
                    for (int j = 0; j < coordinates.size(); j++) {
                        JSONArray polygonCoordinates = coordinates.getJSONArray(j);
                        allCoordinates.addAll(parsePolygonCoordinates(polygonCoordinates));
                    }
                }
                // 还可以处理其他类型，如Point、MultiPoint、LineString、MultiLineString等
            }

            // 输出最大和最小 Latitude 和 Longitude
            double[] minPoint = getMinLatitudeLongitude(allCoordinates);
            double[] maxPoint = getMaxLatitudeLongitude(allCoordinates);
            System.out.println("Min Latitude: " + minPoint[1] + ", Min Longitude: " + minPoint[0]);
            System.out.println("Max Latitude: " + maxPoint[1] + ", Max Longitude: " + maxPoint[0]);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readJSONFile(File file) throws IOException {
        StringBuilder jsonString = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                jsonString.append(line);
            }
        }
        return jsonString.toString();
    }

    public static List<List<List<double[]>>> parsePolygonCoordinates(JSONArray coordinates) {
        List<List<List<double[]>>> polygons = new ArrayList<>();
        List<List<double[]>> polygon = new ArrayList<>();
        for (int i = 0; i < coordinates.size(); i++) {
            JSONArray ring = coordinates.getJSONArray(i);
            List<double[]> points = new ArrayList<>();
            for (int j = 0; j < ring.size(); j++) {
                JSONArray point = ring.getJSONArray(j);
                double longitude = point.getDoubleValue(0);
                double latitude = point.getDoubleValue(1);
                points.add(new double[]{longitude, latitude});
            }
            polygon.add(points);
        }
        polygons.add(polygon);
        return polygons;
    }

    public static double[] getMinLatitudeLongitude(List<List<List<double[]>>> allCoordinates) {
        double minLatitude = Double.MAX_VALUE;
        double minLongitude = Double.MAX_VALUE;

        for (List<List<double[]>> coordinates : allCoordinates) {
            for (List<double[]> ring : coordinates) {
                for (double[] point : ring) {
                    double latitude = point[1];
                    double longitude = point[0];

                    minLatitude = Math.min(minLatitude, latitude);
                    minLongitude = Math.min(minLongitude, longitude);
                }
            }
        }

        return new double[]{minLongitude, minLatitude};
    }

    public static double[] getMaxLatitudeLongitude(List<List<List<double[]>>> allCoordinates) {
        double maxLatitude = Double.MIN_VALUE;
        double maxLongitude = Double.MIN_VALUE;

        for (List<List<double[]>> coordinates : allCoordinates) {
            for (List<double[]> ring : coordinates) {
                for (double[] point : ring) {
                    double latitude = point[1];
                    double longitude = point[0];

                    maxLatitude = Math.max(maxLatitude, latitude);
                    maxLongitude = Math.max(maxLongitude, longitude);
                }
            }
        }

        return new double[]{maxLongitude, maxLatitude};
    }
}
